package com.kcsj.controller.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kcsj.domain.user.RoleAssResource;
import com.kcsj.service.user.RoleAssResourceService;
import com.kcsj.util.JSONUtil;
import com.kcsj.util.MessageHelper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
/**
 * @author 胡庆阳
 * 29th Nov. 2020
 */
@Slf4j
@WebServlet("/permission")
public class Permission extends HttpServlet {
    //查找菜单权限
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //获取前端请求的信息
        String id = request.getParameter("id");
        try {
            this.responseResource(Integer.parseInt(id), response);
        }catch (SQLException e){
            JSONObject message = MessageHelper.dateBaseError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }catch(Exception e){
            JSONObject message = MessageHelper.netWordError();
            e.printStackTrace();
            //响应message到前端
            response.getWriter().println(message);
        }
    }

    //修改resource
    @Override
    protected void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String json = JSONUtil.getJSON(request);
        //创建JSON对象message，以便往前端响应信息
        try {
            if(json.equals("")){
                log.info("未收到请求信息！");
                JSONObject message = MessageHelper.addMessage("未收到请求信息！");
                response.getWriter().println(message);
            }else {
                //将JSON字串解析为RoleAssResource对象
                RoleAssResource roleAssResource = JSON.parseObject(json, RoleAssResource.class);
                //到数据库表修改RoleAssResource对象对应的记录
                RoleAssResourceService.getInstance().update(roleAssResource);
                log.info("roleAssResource修改成功");
                JSONObject message = MessageHelper.addMessage("修改成功");
                response.getWriter().println(message);
            }
        }catch (SQLException e){
            log.error(e.getMessage());
            JSONObject message = MessageHelper.dateBaseError();
            response.getWriter().println(message);
        }catch(Exception e){
            e.printStackTrace();
            log.error(e.getMessage());
            JSONObject message = MessageHelper.netWordError();
            response.getWriter().println(message);
        }
    }

    private void responseResource(int id, HttpServletResponse response)
            throws ServletException, IOException, SQLException {
        //根据id查找学生
        Collection<Integer> allAuthorityIds = RoleAssResourceService.getInstance().findRoleResourceAndAuthorityIds(id);
        String json = JSON.toJSONString(allAuthorityIds);
        JSONObject data = MessageHelper.addData(json);
        //响应department_json到前端
        response.getWriter().println(data);
    }

}
